package com.attendance.chdingtask.service;

import com.alibaba.fastjson.JSONObject;
import com.attendance.chdingtask.controller.MdCallBackController;
import com.attendance.chdingtask.model.MdDetails;
import com.attendance.chdingtask.model.MdDevice;
import com.attendance.chdingtask.model.UserInfo;
import com.attendance.chdingtask.utils.DateUtils;
import com.attendance.chdingtask.utils.Logger;
import com.attendance.chdingtask.utils.common.Result;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class IMdCallBackService  {

    @Autowired
    private MdDeviceService deviceService;

    @Autowired
    private MdDetailsService detailsService;

    @Autowired
    private UserInfoService userInfoService;

    private static Logger logger=Logger.getLogger(IMdCallBackService.class);
    public Result receiveCallback(String nonce, String timestamp, String signature, String signVersion, String orgId, JSONObject json) {
        if (!json.containsKey("callbackTag")) {
            logger.error("**************回调事件参数：json缺少callbackTag*************回调json为{}：", json);
            return Result.error("参数有误");
        } else {
            try{
                String callbackTag = json.getString("callbackTag");
                if ("REC_SUCCESS".equals(callbackTag)) {
                    JSONObject dataJson = json.getJSONObject("data");
                    String deviceSn = dataJson.getString("deviceSn"); // 魔点设备SN码
                    Date date = dataJson.getDate("recognizeTime");    // 打卡时间
                    String userId = dataJson.getString("memberId"); // 人员钉钉组织UserId
                    MdDevice device=deviceService.findBySn(deviceSn);
                    MdDetails details=new MdDetails();
                    details.setDeviceId(device.getId());
                    details.setAreaId(device.getAreaId());
                    details.setAreaName(device.getAreaName());
                    details.setCorpId(device.getCorpId());
                    details.setDeviceSn(deviceSn);
                    details.setFlag(device.getFlag());
                    details.setDeviceName(device.getDeviceName());
                    details.setRecognizeTime(date);
                    details.setUserId(userId);
                    UserInfo info=userInfoService.findByUserId(userId);
                    details.setUserName(info.getName());
                    details.setUnionid(info.getUnionid());
                    details.setDeptId(info.getDepartment());
                    details.setDeptName(info.getDepartmentName());
                    details.setMobile(info.getMobile());
                    String dateStr= DateUtils.getDayToString(date);
                    details.setMdDate(dateStr);
                    details.setYear(dateStr.substring(0,4));
                    details.setMonth(dateStr.substring(5,7));
                    details.setMdTime(dateStr.substring(11));
                    detailsService.save(details);
                }
            }catch ( Exception e){
                e.printStackTrace();
            }

        }
        return Result.success();

    }
}
